package com.google.agerasample.testapp;

import com.google.agerasample.ResourceTable;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class NotesItemProvider extends RecycleItemProvider {
    private Context context;
    private List<NoteGroup> notes;

    public NotesItemProvider(Context context, List<NoteGroup> notes) {
        this.context = context;
        this.notes = notes;
    }

    public void setDataRefresh(List<NoteGroup> notes) {
        this.notes.clear();
        this.notes.addAll(notes);
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return notes.size();
    }

    @Override
    public NoteGroup getItem(int position) {
        return notes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        Component rootLayout =
                LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item_notes, null, false);
        if (!(rootLayout instanceof ComponentContainer)) {
            return null;
        }
        ListContainer lc = (ListContainer) rootLayout.findComponentById(ResourceTable.Id_sublistcontainer);
        NoteItemProvider itemProvider = new NoteItemProvider(context, notes.get(position).getNotes());
        lc.setItemProvider(itemProvider);
        return rootLayout;
    }
}
